#include "LexicalAnalyzerPCH.h"
#include "FiniteStateMachine.h"
#include "State.h"

CFiniteStateMachine::CFiniteStateMachine( const CState* const initialState )
	: c_pInitialState(initialState)
{
	m_pCurrentState = c_pInitialState;
}


CFiniteStateMachine::~CFiniteStateMachine(void)
{
	delete c_pInitialState;
}

CFiniteStateMachine::TRANSITION_RESULT CFiniteStateMachine::ActivateTransition( CHAR trigger ) {

	TRANSITION_RESULT ret;
	const CState* newState = m_pCurrentState->Activate( trigger );

	if ( newState == NULL ) {
		if ( m_pCurrentState->IsFinal() == true ) {
			ret = TR_FINISHED;
		}
		else {
			ret = TR_FAILED;
		}
		newState = c_pInitialState;
	}
	else {
		ret = TR_SUCCEED;
	}

	m_pCurrentState = newState;
	return ret;
}